<?php
declare(strict_types=1);

namespace App\Controller\Admin\V1\Material;

use App\Controller\Admin\AdminController;
use App\Repositories\MaterialAssignRepository;
use App\Repositories\MaterialProjectRepository;
use App\Repositories\MaterialProjectSheetRepository;
use App\Repositories\MaterialRepository;
use App\Request\StoreMaterialProjectRequest;
use Hyperf\DbConnection\Db;

class ProjectController extends AdminController
{
    public function list(){
        $name = $this->request->input('name',null);
        $rows = $this->request->input('rows',10);
        $material_id = $this->request->input('material_id',0);
        $search=[];
        if($name){
            $search['name']=[
                'field' => 'name',
                'value' => $name,
                'method' => 'like'
            ];
        }
        $search['material_id']=[
            'field' => 'material_id',
            'value' => $material_id
        ];
        $orderBy= [
            [
                'field' => 'listorder',
                'sort' => 'asc'
            ]
        ];
        $datas = MaterialProjectRepository::getInstance()->getList('*',$search,['limit'=>$rows],$orderBy);
        if(!empty($datas['result'])){
            foreach ($datas['result'] as &$item){
                $item['sheet_count'] = MaterialProjectSheetRepository::getInstance()->count('id',[[
                    'field' => 'project_id',
                    'value' => $item['id']
                ]]);
            }
        }
        return $this->response->success($datas);
    }

    public function detail(){
        $id = $this->request->input('id');
        $project = MaterialProjectRepository::getInstance()->getById($id);
        $project->material = MaterialRepository::getInstance()->one(['id','title'],[[
            'field' => 'id',
            'value' => $project->material_id
        ]]);
        return $this->response->success($project);
    }

    public function delete(){
        $id = $this->request->input('id');
        $project = MaterialProjectRepository::getInstance()->getById($id);
        $count = MaterialAssignRepository::getInstance()->count('id',[[
            'field' => 'material_id',
            'value' => $project->material_id
        ]]);
        if($count){
            return $this->response->error(10020,'清单已分配单位');
        }
        $result = MaterialProjectRepository::getInstance()->delete($id);
        if($result){
            return $this->response->success($result['data']);
        }
        return $this->response->error(10000,$result['msg']);
    }

    public function store(StoreMaterialProjectRequest $request){
        $admin = $this->request->getAttribute('admin');
        $params = $request->all();
        $params['admin_id'] = $admin->id;
        $count = MaterialAssignRepository::getInstance()->count('id',[[
            'field' => 'material_id',
            'value' => $params['material_id']
        ]]);
        if($count){
            return $this->response->error(10020,'清单已分配单位');
        }
        DB::beginTransaction();
        try{
            $result = MaterialProjectRepository::getInstance()->store($params);
            if(!$result['success']){
                throw new \Exception('项目保存失败');
            }
            $project = $result['data'];
            DB::commit();
        }catch(\Throwable $e){
            DB::rollBack();
            return $this->response->error(10012,$e->getMessage());
        }
        return $this->response->success([
            'id' => $project->id
        ]);
    }

}